﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ShowRadar
{
    public partial class Frm_Adjust : Form
    {
        Form1 form1 = null;
        public Frm_Adjust()
        {
            InitializeComponent();
        }

        private void Frm_Adjust_Load(object sender, EventArgs e)
        {
            form1=this.Owner as Form1;
            for (int i = 0; i < 12; i++)
            {
                // 创建新行
                DataGridViewRow row = new DataGridViewRow();
                // 如果列已定义，可以创建单元格
                row.CreateCells(dgv_AAdjust); // 根据DataGridView的列结构创建单元格

                string name = (50+i*50)+ "mm标定";
                row.Cells[0].Value = name;
                row.Cells[1].Value = false;
                row.Cells[2].Value = "标定";
                dgv_AAdjust.Rows.Add(row);
            }
           
            dgv_AAdjust.CellContentClick += dgv_AAdjust_CellContentClick;
        }

        private void GetAdjust()
        {
            form1.sendCommond(_指令.模块标定状态读取);
        }
        private void ReflashRes()
        {
            if (form1.AdjustBools.Length==0)
            {
                return;
            }
            for (int i = 0; i < dgv_AAdjust.Rows.Count; i++)
            {
                dgv_AAdjust.Rows[i].Cells[1].Value = form1.AdjustBools[i];
            }

        }
        private void dgv_AAdjust_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // 确保不是标题行并且点击的是按钮列
            if (e.RowIndex >= 0 && dgv_AAdjust.Columns[e.ColumnIndex] is DataGridViewButtonColumn)
            {
                // 获取当前行
                DataGridViewRow row = dgv_AAdjust.Rows[e.RowIndex];
                int RowIndex = e.RowIndex+1;
                // 获取按钮所在列的名称
                string columnName = dgv_AAdjust.Columns[e.ColumnIndex].Name;

                // 根据列名处理不同按钮
                if (columnName == "Col_CMD")
                {

                    SendAdjust(RowIndex);
                }

                // 如果需要刷新某行数据，可以这样（可选）
                // dataGridView1.Refresh();
            }
        }

        private void SendAdjust(int CmdIndex)
        {

            List<byte> addCmd = new List<byte>();
            addCmd.Add((byte)CmdIndex);

            form1.sendCommond(_指令.出厂校准, addCmd);
        }

        private void Frm_Adjust_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void Frm_Adjust_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible) 
            {
                GetAdjust();
                Thread.Sleep(200);
                ReflashRes();
            }
           
        }
    }
}
